package com.example.smartcity_poverty.ui.AllService.OA

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.MenuItem
import androidx.recyclerview.widget.LinearLayoutManager
import com.example.smartcity_poverty.R
import com.example.smartcity_poverty.logic.Repository.api
import com.example.smartcity_poverty.logic.Repository.coroutine
import com.example.smartcity_poverty.logic.ServiceBean.OA.OaDataListModel
import com.example.smartcity_poverty.logic.util.Util.show
import com.example.smartcity_poverty.ui.AllService.OA.adapter.OaHomeListAdapter
import kotlinx.android.synthetic.main.activity_oa.*
import kotlinx.coroutines.Dispatchers

/**
 * 门诊预约功能模块描述
 */
class OaActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        supportActionBar?.setDisplayHomeAsUpEnabled(true)
        setContentView(R.layout.activity_oa)
        title = "门诊预约"


        runThread()
    }

    private fun runThread() = coroutine(Dispatchers.IO){
        // 填充数据列表
        setOaHomeList()
    }

    /**
     * 填充数据列表
     */
    private suspend fun setOaHomeList() {
        val array = ArrayList<OaDataListModel.RowsBean>()
        val result = api.getOaDataList()
        if (result.code == 200){
            runOnUiThread {
                array.addAll(result.rows!!)
                oaHomeRecyclerView.apply {
                    this.layoutManager = LinearLayoutManager(context)
                    this.adapter = OaHomeListAdapter(array, R.layout.re_oa_home_list)
                    this.isNestedScrollingEnabled = false
                }
            }
        }else runOnUiThread { result.msg?.show() }
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        finish()
        return super.onOptionsItemSelected(item)
    }
}